/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.AdaccountPopUp;
import com.tbit.uqbike.object.pojo.vo.AdaccountPopUpQueryVO;
import com.tbit.uqbike.object.pojo.vo.AdaccountPopUpVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.business.AdaccountPopUpService;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

import static com.tbit.uqbike.webmanager.controller.BaseController.getLogin;

/**
* 弹框广告
*
* @author yao
* @since 2024-03-28
*/
@RestController
@RequestMapping("/adaccountPopUp")
@AllArgsConstructor
public class AdaccountPopUpController {

    private final HttpServletRequest request;

    private final AdaccountPopUpService adaccountPopUpService;

    /**
     * 分页查询弹框广告
     * @param queryVO 查询条件
     * @return 弹框广告分页
     */
    @GetMapping("/page")
    public JsonResult page(AdaccountPopUpQueryVO queryVO){
        PageInfo<AdaccountPopUp> pageInfo = adaccountPopUpService.page(queryVO);

        PageResult<AdaccountPopUpVO> data = PageResult.convertPage(pageInfo, AdaccountPopUpVO.class);

        long now = new Date().getTime();
        data.getList().forEach(adaccountPopUp -> adaccountPopUp.setStatus(now)); // 设置状态

        return JsonResult.ok(data);
    }

    /**
     * 获得弹框广告
     * @param id 弹框广告编号
     * @return 弹框广告
     */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id") Long id){
        AdaccountPopUp adaccountPopUp = adaccountPopUpService.getById(id);

        long now = new Date().getTime();
        AdaccountPopUpVO vo = BeanUtil.copyProperties(adaccountPopUp, AdaccountPopUpVO.class);
        // 设置状态
        vo.setStatus(now);
        return JsonResult.ok(vo);
    }

    /**
     * 保存弹框广告
     * @param vo 弹框广告
     * @return success
     */
    @PostMapping
    @OperateLog(module = ModuleEnum.ADVERT, methods = MethodEnum.ADD_ADVERT)
    public JsonResult save(@RequestBody @Validated(value = Insert.class) AdaccountPopUpVO vo){

        LoginInfo loginInfo = getLogin(request);
        vo.setCreator(loginInfo.getName());

        adaccountPopUpService.save(vo);

        return JsonResult.ok();
    }

    /**
     * 更新弹框广告
     * @param vo 弹框广告
     * @return success
     */
    @PutMapping
    @OperateLog(module = ModuleEnum.ADVERT, methods = MethodEnum.UPDATE_ADVERT)
    public JsonResult update(@RequestBody @Validated(value = Update.class) AdaccountPopUpVO vo){

        LoginInfo loginInfo = getLogin(request);
        vo.setUpdater(loginInfo.getName());

        adaccountPopUpService.update(vo);

        return JsonResult.ok();
    }

    /**
     * 删除弹框广告
     * @param idList 弹框广告编号列表
     * @return success
     */
    @DeleteMapping
    @OperateLog(module = ModuleEnum.ADVERT, methods = MethodEnum.DEL_ADVERT)
    public JsonResult delete(@RequestBody List<Long> idList){
        adaccountPopUpService.delete(idList);

        return JsonResult.ok();
    }

    /**
     * 同步弹框广告
     * @return success
     */
    // @PostMapping("/sync")
    @Deprecated
    public JsonResult sync(){
        LoginInfo loginInfo = getLogin(request);

        adaccountPopUpService.sync(loginInfo);

        return JsonResult.ok();
    }
}